<template>
  <div class="app-container">
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="580px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        style="margin-left: 55px"
      >
        <el-col :span="24">
          <el-form-item label="物料代码" prop="bomWldm">
            <el-input
              v-model="queryParams.bomWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态标志" prop="bomZtbz">
            <el-select
              v-model="queryParams.bomZtbz"
              placeholder="请选择状态标志"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in bomZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="采购属性" prop="bomCgsx">
            <el-select
              v-model="queryParams.bomCgsx"
              placeholder="请选择采购属性"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in bomCgsxOptionsNromal"
                :key="dict.cgId"
                :label="dict.cgName"
                :value="dict.cgId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="综合查询" prop="itmPmgg">
            <el-input
              v-model="queryParams.itmPmgg"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="建立日期" prop="bomShrq">
            <!--            <el-date-picker clearable size="small"-->
            <!--                            v-model="queryParams.bomShrq"-->
            <!--                            type="date"-->
            <!--                            value-format="yyyy-MM-dd"-->
            <!--                            placeholder="选择审核日期">-->
            <!--            </el-date-picker>-->
            <el-date-picker
              clearable
              size="small"
              v-model="dateRange"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              :clearable="false"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" style="position: relative; margin-bottom: 5px">
      <el-col :span="18" style="display: flex; align-items: flex-start">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearchs"
          >搜索</el-button
        >
        <el-button
          class="newAdd"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:bom:add']"
        >
          新增
        </el-button>
        <el-button
          class="newAdd"
          type="primary"
          plain
          icon="el-icon-money"
          size="mini"
          :disabled="single"
          @click="handleCopy"
          v-hasPermi="['project:bom:add']"
        >
          拷贝
        </el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdateTjFlag"
          v-hasPermi="['project:bom:submit']"
        >
          提交
        </el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['project:bom:remove']"
        >
          删除
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:bom:export']"
        >
          导出
        </el-button>
        <Tag type="dot" color="#29abdf">新增</Tag>
        <Tag type="dot" color="#06c420">提交</Tag>
        <Tag type="dot" color="#bb0331">审核</Tag>
        <Tag type="dot" color="#d39207">变更</Tag>
      </el-col>

      <el-col :span="6" style="text-align: right">
        <Page
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <split-pane
      :min-percent="10"
      :default-percent="50"
      split="horizontal"
      style="height: calc(100vh - 200px)"
    >
      <template #paneL>
        <el-table
          :row-style="iRowStyle"
          :cell-style="iCellStyle"
          :header-row-style="iHeaderRowStyle"
          :header-cell-style="iHeaderCellStyle"
          v-loading="loading"
          :data="bomList"
          stripe
          border
          height="100%"
          highlight-current-row
          size="mini"
          ref="multipleTable"
          @row-click="getChildList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column fixed type="selection" width="55" align="center" />
          <el-table-column
            label="物料代码"
            width="125"
            align="center"
            prop="bomWldm"
          />

          <el-table-column
            label="客户编码"
            width="125"
            align="center"
            prop="itmCustwldm"
          />

          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="350"
            prop="itmPmgg"
          />

          <el-table-column
            label="采购属性"
            align="center"
            width="80"
            show-overflow-tooltip
            prop="bomCgsx"
          >
            <template #default="scope">
              <dict-tag :options="bomCgsxOptions" :value="scope.row.bomCgsx" />
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            show-overflow-tooltip
            align="center"
            prop="remark"
          />

          <el-table-column
            label="单据状态"
            align="center"
            width="80"
            prop="bomZtbz"
          >
            <template #default="scope">
              <dict-tag :options="bomZtbzOptions" :value="scope.row.bomZtbz" />
            </template>
          </el-table-column>
          <el-table-column
            label="创建人员"
            width="80"
            align="center"
            prop="createBy"
          />
          <el-table-column
            label="创建时间"
            show-overflow-tooltip
            align="center"
            prop="createTime"
            width="150"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="修改人员"
            width="80"
            align="center"
            prop="updateBy"
          />
          <el-table-column
            label="修改时间"
            show-overflow-tooltip
            align="center"
            prop="updateTime"
            width="150"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="审核人员"
            width="80"
            align="center"
            prop="bomShry"
          />
          <el-table-column
            label="审核日期"
            align="center"
            prop="bomShrq"
            width="150"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.bomShrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            fixed="right"
            width="170"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                :disabled="scope.row.bomTjflag === '1'"
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="handleAddChild(scope.row)"
                v-hasPermi="['project:bom:addChild']"
              >
                添加子项
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['project:bom:edit']"
              >
                修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template #paneR>
        <el-table
          :row-style="iRowStyle"
          :cell-style="iCellStyle"
          :header-row-style="iHeaderRowStyle"
          :header-cell-style="iHeaderCellStyle"
          :row-class-name="rowClass"
          class="childTable"
          height="100%"
          style="width: 100%"
          stripe
          border
          size="mini"
          highlight-current-row
          :data="childBomList"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="层级"
            width="55px"
            align="left"
            prop="level"
          />
          <el-table-column
            label="物料代码"
            width="125px"
            show-overflow-tooltip
            align="center"
            prop="bodZxdm"
          />
          <el-table-column
            label="客户编码"
            width="125px"
            show-overflow-tooltip
            align="center"
            prop="itmCustomeWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="350px"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            width="60"
            show-overflow-tooltip
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="委外发料标识"
            width="100"
            align="center"
            prop="bodWwbz"
          >
            <template #default="scope">
              <dict-tag :options="bodWwbzOptions" :value="scope.row.bodWwbz" />
            </template>
          </el-table-column>
          <el-table-column
            label="采购属性"
            width="80"
            align="center"
            prop="bodCgsx"
          >
            <template #default="scope">
              <dict-tag :options="bomCgsxOptions" :value="scope.row.bodCgsx" />
            </template>
          </el-table-column>
          <el-table-column
            label="单位用量"
            width="80"
            align="center"
            prop="bodDwyl"
          />
          <el-table-column
            label="单件净重"
            width="80"
            align="center"
            prop="itmJz"
          />
          <el-table-column
            label="单件水口"
            width="80"
            align="center"
            prop="itmSkzl"
          />
          <el-table-column
            label="单件毛重"
            width="80"
            align="center"
            prop="itmMz"
          />
          <el-table-column
            label="主成型标识"
            width="100"
            align="center"
            prop="bodMain"
          >
            <template #default="scope">
              <dict-tag :options="bodWwbzOptions" :value="scope.row.bodMain" />
            </template>
          </el-table-column>
          <el-table-column
            label="父项代码"
            width="140px"
            align="center"
            prop="bodFxdm"
          />
          <el-table-column
            label="父项客户编码"
            width="140px"
            align="center"
            prop="itmFxCustwldm"
          />
          <el-table-column
            label="备注"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="remark"
          />

          <el-table-column
            label="变更单号"
            show-overflow-tooltip
            width="130"
            align="center"
            prop="bodBgdh"
          />

          <el-table-column
            label="ENC单号"
            show-overflow-tooltip
            width="80"
            align="center"
            prop="bodEcnno"
          />

          <el-table-column
            label="变更备注"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="bodDesc"
          />

          <el-table-column
            label="操作"
            width="170"
            fixed="right"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <div v-show="bomWldms.bomZtbz != 2">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-plus"
                  @click="handleAddChild(scope.row)"
                  v-hasPermi="['project:bom:addChild']"
                >
                  新增
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleBodUpdate(scope.row)"
                  v-hasPermi="['project:bom:edit']"
                >
                  修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleBodDelete(scope.row)"
                  v-hasPermi="['project:bom:remove']"
                >
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </split-pane>
    <!--
    子项Table
    -->

    <!-- 添加或修改bom资料维护对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="600px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="20">
          <el-form-item label="物料代码" prop="bomWldm">
            <el-select
              v-model="form.bomWldm"
              :filter-method="getWldmListTOP50"
              @change="geBomWldm"
              filterable
              placeholder="请选择物料代码"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in wldmList"
                :label="dict.itmWldm"
                :key="dict.itmWldm"
                :value="dict.itmWldm"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button
            class="addButton"
            @click="clickAddButton"
            type="primary"
            icon="el-icon-plus"
            circle
          ></el-button>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品名规格" prop="bomPmgg">
            <el-input v-model="form.itmPmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="采购属性" prop="bomCgsx">
            <el-select v-model="form.bomCgsx" placeholder="请选择采购属性">
              <el-option
                v-for="dict in bomCgsxOptionsNromal"
                :key="dict.cgId"
                :label="dict.cgName"
                :value="dict.cgId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加子项代码对话框 -->
    <el-dialog
      :title="title"
      v-model="addChildView"
      :close-on-click-modal="false"
      draggable
      width="600px"
      append-to-body
    >
      <el-form
        ref="childForm"
        :model="childForm"
        :rules="childFormRules"
        label-width="100px"
      >
        <el-col :span="24">
          <el-form-item label="父项物料代码" prop="bodFxdm">
            <el-input v-model="childForm.bodFxdm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品名规格" prop="bodPmgg">
            <el-input v-model="childForm.bomPmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="子项代码" prop="bodZxdm">
            <el-select
              v-model="childForm.bodZxdm"
              filterable
              remote
              value-key="itmWldm"
              clearable
              placeholder="请选择子项代码"
              @change="changeZxpm"
              :remote-method="remoteMethod"
              :loading="loadingPmgg"
            >
              <el-option
                v-for="item in optionsPmgg"
                :key="item.itmWldm"
                :label="item.itmWldm"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button
            class="addButton"
            @click="clickAddButton"
            type="primary"
            icon="el-icon-plus"
            circle
          ></el-button>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品名规格" prop="bodPmgg">
            <el-select
              v-model="childForm.itmPmgg"
              filterable
              remote
              value-key="itmPmgg"
              clearable
              placeholder="请输入关键词"
              @change="changeZxpm"
              :remote-method="remoteMethod"
              :loading="loadingPmgg"
              style="width: 100%"
            >
              <el-option
                v-for="item in optionsPmgg"
                :key="item.itmWldm"
                :label="item.itmPmgg"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位用量" prop="bodDwyl">
            <el-input v-model="childForm.bodDwyl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位" prop="itmDw">
            <el-select v-model="childForm.itmDw" disabled>
              <el-option
                v-for="dict in itmDwOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="采购属性" prop="bodCgsx">
            <el-select v-model="childForm.bodCgsx" placeholder="请选择采购属性">
              <el-option
                v-for="dict in bomCgsxOptionsNromal"
                :key="dict.cgId"
                :label="dict.cgName"
                :value="dict.cgId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="主成型标识" prop="bodMain">
            <el-radio-group v-model="childForm.bodMain">
              <el-radio
                v-for="dict in bodWwbzOptions"
                :key="dict.dictId"
                :label="dict.dictValue"
              >
                {{ dict.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="委外加工标识" prop="bodWwbz">
            <el-radio-group v-model="childForm.bodWwbz">
              <el-radio
                v-for="dict in bodWwbzOptions"
                :key="dict.dictId"
                :label="dict.dictValue"
              >
                {{ dict.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="childForm.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitChildForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改子项代码对话框 -->
    <el-dialog
      :title="title"
      v-model="editChildView"
      :close-on-click-modal="false"
      draggable
      width="600px"
      append-to-body
    >
      <el-form
        ref="childForm"
        :model="childForm"
        :rules="childFormRules"
        label-width="100px"
      >
        <el-col :span="24">
          <el-form-item label="父项物料代码" prop="bodFxdm">
            <el-input v-model="childForm.bodFxdm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="子项代码" prop="bodZxdm">
            <el-input
              v-model="childForm.bodZxdm"
              @blur="getAddBomWldm"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品名规格" prop="bodPmgg">
            <el-input v-model="childForm.itmPmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位用量" prop="bodDwyl">
            <el-input v-model="childForm.bodDwyl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位" prop="itmDw">
            <el-select v-model="childForm.itmDw" disabled>
              <el-option
                v-for="dict in itmDwOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="采购属性" prop="bodCgsx">
            <el-select v-model="childForm.bodCgsx" placeholder="请选择采购属性">
              <el-option
                v-for="dict in bomCgsxOptionsNromal"
                :key="dict.cgId"
                :label="dict.cgName"
                :value="dict.cgId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="主成型标识" prop="bodMain">
            <el-radio-group v-model="childForm.bodMain">
              <el-radio
                v-for="dict in bodWwbzOptions"
                :key="dict.dictId"
                :label="dict.dictValue"
              >
                {{ dict.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="委外加工标识" prop="bodWwbz">
            <el-radio-group v-model="childForm.bodWwbz">
              <el-radio
                v-for="dict in bodWwbzOptions"
                :key="dict.dictId"
                :label="dict.dictValue"
              >
                {{ dict.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="childForm.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit">取 消</el-button>
        <el-button type="primary" @click="submitEditChildForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 拷贝对话框 -->
    <el-dialog
      title="拷贝bom资料"
      v-model="openCopy"
      :close-on-click-modal="false"
      draggable
      width="600px"
      append-to-body
    >
      <el-form
        ref="copyForm"
        :model="copyForm"
        :rules="copyFormRules"
        label-width="100px"
      >
        <el-col :span="24">
          <el-form-item label="拷贝物料代码" prop="oldParentCode">
            <el-input v-model="copyForm.oldParentCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="拷贝品名规格" prop="oldParentDetail">
            <el-input v-model="copyForm.oldParentDetail" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="新物料代码" prop="newParentCode">
            <el-select
              v-model="copyForm.newParentCode"
              :filter-method="getWldmListTOP50"
              @change="geBomWldm2"
              filterable
              placeholder="请选择物料代码"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in wldmList"
                :label="dict.itmWldm"
                :key="dict.itmWldm"
                :value="dict.itmWldm"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button
            class="addButton"
            @click="clickAddButton"
            type="primary"
            icon="el-icon-plus"
            circle
          ></el-button>
        </el-col>
        <el-col :span="24">
          <el-form-item label="新品名规格" prop="newParentDetail">
            <el-input v-model="copyForm.newParentDetail" disabled />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openCopy = false">取 消</el-button>
        <el-button type="primary" @click="submitCopyBom">确 定</el-button>
      </div>
    </el-dialog>

    <!--    引入生产物料组件-->
    <AddButton
      v-if="openDialog"
      ref="testDialog"
      @AddReturn="addReturn"
    ></AddButton>
  </div>
</template>

<script>
import {
  addBom,
  addChildBom,
  copyBomByParentCode,
  delBom,
  deleteBod,
  editBod,
  exportBomParentChild,
  findBodById,
  getBom,
  getChild,
  getItmByWldm,
  judge,
  listBom,
  submitBom,
  updateBom,
} from "@/api/project/bom";
import { getAllCgList, getAllCgListNormal } from "@/api/project/mstr";
import AddButton from "@/views/project/materiel/addButton.vue";
import {
  getItmWldmAndCustWldmList,
  getItmByInfo,
} from "@/api/project/materiel";
import { downloadFileXls } from "@/utils/ruoyi";

export default {
  name: "Bom",
  components: {
    AddButton,
  },
  data() {
    return {
      tableH: 10,
      editChildView: false,
      openDiag: false,
      openSearch: false,
      openDialog: false,
      openCopy: false,
      // 遮罩层
      loading: true,
      //子项Bom
      childBomList: [],
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      dateRange: [
        new Date(new Date().getFullYear() - 20, new Date().getMonth()),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      //单位
      itmDwOptions: [],
      //委外标识
      bodWwbzOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //子项添加
      addChildView: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // bom资料维护表格数据
      bomList: [],
      // 弹出层标题
      title: "",
      bomWldm: [],
      //保存Bom物料代码
      bomWldms: null,
      tj: [],
      // 是否显示弹出层
      open: false,
      // 状态标志字典
      bomZtbzOptions: [],
      //采购属性字典
      bomCgsxOptions: [],
      //正常采购属性字典
      bomCgsxOptionsNromal: [],
      itmwldmOptionsDatas: [],
      wldmList: [],
      ZXwldmList: [],
      optionsPmgg: [],
      loadingPmgg: false,
      // 选中数据
      selectionData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bomWldm: null,
        itmPmgg: null,
        bomZtbz:
          this.$route.fullPath.indexOf("/projectDataSelect/") > 1 ? "2" : null,
        bomCgsx: null,
        bomShry: null,
        bomShrq: null,
        beginDate: null,
        endDate: null,
      },
      // 表单参数
      form: {},
      // 孩子表单
      childForm: {},
      // 拷贝表单
      copyForm: { newParentCode: undefined, newParentDetail: undefined },
      // 表单校验
      rules: {
        bomWldm: [
          { required: true, message: "物料代码不能为空", trigger: "blur" },
        ],
        bomCgsx: [
          { required: true, message: "采购属性不能为空", trigger: "blur" },
        ],
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      childFormRules: {
        bodZxdm: [
          { required: true, message: "子项代码不能为空", trigger: "blur" },
        ],
        bodCgsx: [
          { required: true, message: "采购属性不能为空", trigger: "blur" },
        ],
        bodDwyl: [
          { required: true, message: "单位用量不能为空", trigger: "blur" },
        ],
      },
      copyFormRules: {
        newParentCode: [
          { require: true, message: "物料代码不能为空", trigger: "blur" },
        ],
      },
    };
  },

  created() {
    this.getList();
    this.getDicts("sys_bom_ztbz").then((response) => {
      this.bomZtbzOptions = response.data;
    });
    this.getDicts("sys_unit").then((response) => {
      this.itmDwOptions = response.data;
    });
    this.getDicts("sys_flag").then((response) => {
      this.bodWwbzOptions = response.data;
    });
    getItmWldmAndCustWldmList().then((res) => {
      this.itmwldmOptionsDatas = res.data;
    });
    //将不是字典的集合转为字典
    getAllCgList().then((response) => {
      this.bomCgsxOptions = response.data;
      for (let i of this.bomCgsxOptions) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });
    getAllCgListNormal().then((response) => {
      this.bomCgsxOptionsNromal = response.data;
      for (let i of this.bomCgsxOptionsNromal) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });
  },
  methods: {
    rowClass({ row, rowIndex }) {
      if (row.bodZtbz === "2" && row.bodBgdh) return "update";
      if (row.bodZtbz === "1") return "insert";
    },
    iRowStyle: function ({ row, rowIndex }) {
      return "height:10px;";
    },
    iHeaderRowStyle: function ({ row, rowIndex }) {
      return "height:10px;";
    },
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //新增状态
      if (row.bomTjflag === "0" && row.bomZtbz === 1) {
        return "padding:0px 0;color:rgb(0, 126, 255)";
      }
      //提交状态
      else if (row.bomTjflag === "1" && row.bomZtbz === 1) {
        return "padding:0px 0;color:rgb(15, 152, 86)";
      }
      //审核状态
      else if (row.bomZtbz === 2) {
        return "padding:0px 0;color:#bb0331";
      } else {
        return "padding:0px 0";
      }
    },
    iHeaderCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      return "padding:0px;text-align:center";
    },
    geBomWldm() {
      if (this.form.bomWldm === null || this.form.bomWldm === "") {
        return;
      }
      getItmByWldm(this.form.bomWldm).then((response) => {
        if (response.data) {
          this.form.itmPmgg = response.data.itmPmgg;
        } else {
          this.msgError("系统无此物料代码，请新建或者输入正确物料代码");
        }
      });
    },
    geBomWldm2(val) {
      if (!val) {
        this.copyForm.newParentDetail = "";
        return;
      }
      getItmByWldm(val).then((response) => {
        if (response.data) {
          this.copyForm.newParentDetail = response.data.itmPmgg;
        } else {
          this.msgError("系统无此物料代码，请新建或者输入正确物料代码");
        }
      });
    },
    getWldmListTOP50(input) {
      let temp = [];
      let that = this;
      new Promise(function (resolve, reject) {
        for (let i of that.itmwldmOptionsDatas) {
          if (i.itmWldm.indexOf(input) != -1) {
            temp.push(i);
          }
        }
        resolve(temp);
      }).then((res) => {
        that.wldmList = res.slice(0, 50);
      });
    },
    remoteMethod(query) {
      if (query !== "") {
        this.loadingPmgg = true;
        getItmByInfo({ desc: query, pageSize: 30, pageNum: 1 }).then((res) => {
          this.optionsPmgg = res.rows;
          this.loadingPmgg = false;
        });
      } else {
        this.optionsPmgg = [];
      }
    },
    changeZxpm(val) {
      this.childForm.bodZxdm = val.itmWldm;
      this.childForm.itmPmgg = val.itmPmgg;
      this.childForm.itmDw = val.itmDw;
    },
    getAddBomWldm() {
      if (this.childForm.bodZxdm === null || this.childForm.bodZxdm === "") {
        return;
      }
      getItmByWldm(this.childForm.bodZxdm).then((response) => {
        if (response.data) {
          this.childForm.itmPmgg = response.data.itmPmgg;
          this.childForm.itmDw = response.data.itmDw;
        } else {
          this.msgError("系统无此物料代码，请新建或者输入正确物料代码");
        }
      });
    },
    //修改子项弹窗
    handleBodUpdate(row) {
      this.reset();
      findBodById(row.bodId).then((response) => {
        this.childForm = response.data;
        this.editChildView = true;
      });
    },
    //修改子项
    submitEditChildForm() {
      this.$refs["childForm"].validate((valid) => {
        if (valid) {
          if (this.childForm.bodDwyl <= 0) {
            this.msgError("单位用量必须大于0");
            return;
          }
          judge(
            this.bomWldms.bomWldm,
            this.childForm.bodZxdm,
            this.childForm.bodCgsx
          ).then((response) => {
            if (response.data) {
              editBod(this.childForm).then((response) => {
                this.msgSuccess("修改成功");
                this.editChildView = false;
                this.getList(this.bomWldms);
              });
            } else {
              this.msgError("一个Bom结构只允许出现一个成型物料");
            }
          });
        }
      });
    },
    //添加生产物料
    clickAddButton() {
      this.openDialog = true;
      this.$nextTick(() => {
        this.$refs.testDialog;
      });
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 查询bom资料维护列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.bomShrq = null;
      this.tableH = (document.body.clientHeight - 160) / 2;
      this.queryParams.pageSize = this.tableH > 350 ? 14 : 10;
      listBom(this.queryParams).then((response) => {
        this.bomList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.childBomList = [];
        if (row != null) {
          this.getChildList(row);
        } else if (this.bomList != null && this.bomList.length > 0) {
          this.getChildList(this.bomList[0]);
        }
      });
    },
    //查询子项
    getChildList(row) {
      //点击行触发，选中或不选中复选框
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
      this.bomWldms = row;
      if (row.bomWldm != null) {
        getChild(row.bomWldm).then((response) => {
          this.childBomList = response.data;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.addChildView = false;
      this.open = false;
      this.reset();
      this.openSearch = false;
    },
    // 取消按钮
    cancelDiag() {
      this.openDiag = false;
      this.reset();
    },
    cancelEdit() {
      this.editChildView = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bomId: null,
        bomWldm: null,
        bomZtbz: 1,
        bomTjflag: null,
        bomCgsx: "",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        bomShry: null,
        bomShrq: null,
        remark: null,
        itmPmgg: null,
      };
      this.resetForm("form");
      this.childForm = {
        bodId: null,
        bodFxdm: null,
        bodZxdm: null,
        bodDwyl: null,
        bodCgsx: "",
        itmPmgg: null,
        remark: null,
        bomPmgg: null,
        bodWwbz: "1",
        bodMain: null,
      };
      this.resetForm("childForm");
      this.resetForm("copyForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.dateRange != null) {
        this.queryParams.beginDate = this.moment(this.dateRange[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.queryParams.endDate = this.moment(this.dateRange[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      this.openSearch = false;
      this.queryParams.pageNum = 1;
      this.openDiag = false;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bomId);
      this.bomWldm = selection.map((item) => item.bomWldm);
      this.tj = selection.map((item) => item.bomTjflag);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selectionData = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加bom资料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bomId = row.bomId || this.ids;
      getBom(bomId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改bom资料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bomId != null) {
            updateBom(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBom(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交拷贝 */
    submitCopyBom() {
      this.$refs["copyForm"].validate((valid) => {
        if (valid) {
          copyBomByParentCode(this.copyForm).then((res) => {
            this.msgSuccess(res.msg);
            this.openCopy = false;
            this.getList();
          });
        }
      });
    },
    //提交子项物料代码
    submitChildForm() {
      this.$refs["childForm"].validate((valid) => {
        if (valid) {
          if (this.childForm.bodDwyl <= 0) {
            this.msgError("单位用量必须大于0");
            return;
          }
          //首先判断该物料结构是否合理
          judge(
            this.bomWldms.bomWldm,
            this.childForm.bodZxdm,
            this.childForm.bodCgsx
          ).then((response) => {
            if (response.data) {
              addChildBom(this.childForm).then((response) => {
                this.msgSuccess("新增成功");
                this.addChildView = false;
                this.getList(this.bomWldms);
              });
            } else {
              this.msgError("一个Bom结构只允许出现一个成型物料");
            }
          });
        }
      });
    },
    //提交审核
    handleUpdateTjFlag(row) {
      if (this.tj[0] === "1") {
        this.msgError("Bom已经提交审核，不允许再次提交");
        return;
      }
      const bomIds = row.bomId || this.ids;
      const bomWldm = row.bomWldm || this.bomWldm;
      this.$confirm(
        '是否确认提交bom资料维护编号为"' + bomWldm + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return submitBom(bomIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("提交成功");
        })
        .catch(() => {});
    },
    handleSearchs() {
      this.reset();
      this.title = "查询bom资料维护";
      this.openSearch = true;
    },
    handleSearch() {
      this.reset();
      this.openDiag = true;
    },
    /** 拷贝窗口打开事件 */
    handleCopy() {
      this.copyForm.oldParentCode = this.selectionData[0].bomWldm;
      this.copyForm.oldParentDetail = this.selectionData[0].itmPmgg;
      this.copyForm.newParentCode = null;
      this.copyForm.newParentDetail = null;
      this.openCopy = true;
    },
    //添加子项
    handleAddChild(row) {
      this.reset();
      if (row.bomWldm) {
        this.childForm.bodFxdm = row.bomWldm;
      }
      if (row.itmPmgg) {
        this.childForm.bomPmgg = row.itmPmgg;
      }
      if (row.bodZxdm) {
        this.childForm.bodFxdm = row.bodZxdm;
      }
      this.addChildView = true;
    },
    //删除子项
    handleBodDelete(row) {
      this.$confirm(
        '是否确认删除子项代码为"' + row.bodZxdm + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return deleteBod(row.bodId);
        })
        .then(() => {
          this.getList(this.bomWldms);
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const bomIds = row.bomId || this.ids;
      const bomWldm = row.bomWldm || this.bomWldm;
      this.$confirm(
        '是否确认删除物料代码为"' + bomWldm + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delBom(bomIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm(
        "是否确认导出bom资料维护数据项?（不勾选就导出全部bom资料)",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.exportLoading = true;
          const bomIds = this.ids;
          return exportBomParentChild(bomIds);
        })
        .then((response) => {
          // this.download(response.msg)
          downloadFileXls(response, "bom父子项数据");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    addReturn(addForm) {
      if (addForm.itmWldm) {
        this.copyForm.newParentCode = addForm.itmWldm;
        this.geBomWldm2(addForm.itmWldm);
      } else {
        this.copyForm.newParentCode = null;
        this.copyForm.newParentDetail = null;
      }

      this.openDialog = false;
    },
  },
};
</script>
<style scoped>
.addButton {
  margin-left: 15px;
}
.app-container {
  position: relative;
  display: block;
  height: 20%;
}
.el-table .a {
  color: #3ac63a;
}
</style>
